package com.whoimi.admin.rest;

import com.whoimi.common.ResultInfo;
import com.whoimi.common.jdbc.JdbcPageQueryHelper;
import com.whoimi.admin.entity.CurrentUserDTO;
import com.whoimi.admin.entity.User;
import com.whoimi.admin.rest.req.UserPageQuery;
import com.whoimi.admin.service.UserService;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/v1/users")
public class UserRest {
    private static final Logger log = LoggerFactory.getLogger(UserRest.class);
    @Resource
    private UserService userService;

    @Resource
    private JdbcPageQueryHelper jdbcPageQueryHelper;

    @GetMapping("/me")
    public ResultInfo<CurrentUserDTO> getCurrentUser() {
        CurrentUserDTO currentUserDTO = userService.getCurrentUserInfo("admin");
        return ResultInfo.ok(currentUserDTO);
    }

    @GetMapping("/page")
    public ResultInfo<Page<User>> getUserPage(@Valid UserPageQuery queryParams, Pageable pageable) {
        log.info("queryParams:{},pageRequest {}", queryParams, pageable);
//        queryParams.setUsername("admin");
/*        String sql = """
                            SELECT
                              *
                            FROM
                              wiic_admin.sys_user
                """;
              */
        Page<User> result = jdbcPageQueryHelper.buildPageWithTableName("sys_user", queryParams, pageable, new BeanPropertyRowMapper<>(User.class));
        return ResultInfo.ok(result);
    }

    /**
     * 删除用户
     */
    @DeleteMapping("/{ids}")
    public ResultInfo<Void> deleteUsers(@PathVariable String ids) {
        userService.deleteAllById(ids);
        return ResultInfo.ok();
    }

}